package com.wyj.service.imp;

import com.wyj.mapper.CommentMapper;
import com.wyj.pojo.Comment;
import com.wyj.service.CommentService;
import com.wyj.util.CommunityConstant;
import com.wyj.util.SensitiveFilter;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;


/**
 * @author 王永杰
 * @Description 描述该类的做什么用
 * @since 2022/7/17
 */
@Service
public class CommentServiceImp implements CommentService , CommunityConstant {

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    SensitiveFilter sensitiveFilter;

    @Autowired
    DiscussPostServiceImp discussPostServiceImp;

    @Override
    public List<Comment> getCommentsByEntity(int entityId, int entityType, int start, int limit) {
        return commentMapper.getCommentsByEntity(entityId, entityType, start, limit);
    }

    @Override
    public int getComments(int entityId, int entityType) {
        return commentMapper.getComments(entityType, entityId);
    }

    @Override
    public int addComment(Comment comment) {
        if (comment == null) {
            throw new IllegalArgumentException("评论的参数为空");
        }

        //对我们的评论内容 进行过滤一下
        comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
        comment.setContent(sensitiveFilter.filter(comment.getContent()));

        int i = commentMapper.addComment(comment);

        if (comment.getEntityType() == ENTITY_TYPE_POST) {
            //在评论的那个表中  因为我们增加了一条评论 那么的话 我们根据实体类型 以及帖子对应的id
            // 那么的话 我们就可以查询出 这个贴子对应多少条评论
            int count = commentMapper.getComments(comment.getEntityType(),comment.getEntityId());
            //帖子的回复数
            int rows = discussPostServiceImp.updateDiscussPostReplies(comment.getEntityId(),count);
        }
        return i;
    }
    @Override
    public Comment getCommentById(int id) {
        return commentMapper.getCommentById(id);
    }
}
